import Ux from "ux";
import React from "react";
import {saveAs} from "file-saver";
import Rdr from './Web.Column';
import {Modal} from "antd";

const yiList = (reference, state = {}, keyRender) => {
    const table = Ux.fromHoc(reference, "table");
    const $table = Ux.clone(table);
    const columns = Ux.configColumn(reference, table.columns);
    columns.forEach(column => {
        const {config = {}} = column;
        // name 渲染
        if ("name" === column.dataIndex) {
            column.sorter = Ux.sorterAscTFn("name");
            column.render = Rdr.renderName(reference);
        }
        // size 渲染
        if ("size" === column.dataIndex) {
            column.sorter = Ux.sorterAscTFn("size");
            column.render = Rdr.renderSize(reference);
        }
        // updatedAt
        if ("updatedAt" === column.dataIndex) {
            column.sorter = Ux.sorterAscDFn("updatedAt");
        }
        // createdBy
        if ("createdBy" === column.dataIndex) {
            column.render = Rdr.renderCreatedBy(reference, config);
        }
        // key 操作列
        if ("key" === column.dataIndex) {
            column.render = keyRender(reference, config);
        }
    })
    $table.columns = columns;
    state.$table = $table;
    return Ux.promise(state);
}
const yoRowSelection = (table = {}, reference, isTrash = false) => {
    const {$selectedKeys = []} = reference.props;      // 选中项
    if (isTrash) {
        // 回收站中的所有内容都可以选中执行返回
        table.rowSelection = {
            selectedRowKeys: $selectedKeys,
            onChange: (keys = []) => Ux.fn(reference).rxRowSelect(keys)
        }
    } else {
        /*
         * 非回收站中的条件
         * 1. 当前记录必须支持 w 操作
         * 2. 该目录本身必须支持 x 操作
         */
        const {$directory = {}} = reference.props;
        const dirVisit = $directory.visitMode ? $directory.visitMode : [];
        if (dirVisit.includes("w")) {
            table.rowSelection = {
                selectedRowKeys: $selectedKeys,
                onChange: (keys = []) => Ux.fn(reference).rxRowSelect(keys),
                getCheckboxProps: (record) => {
                    const {visitMode = []} = record;
                    const props = {};
                    props.disabled = !visitMode.includes("x");
                    return props;
                }
            }
        }
    }
    return table;
}
const rxExecutor = (reference, record) => {
    const info = Ux.fromHoc(reference, "info");
    return ({
        // 重命名文件
        renameFile: () => {
            const state = {};
            state.$visible = true;
            const {$directory = {}} = reference.props;
            const $inited = Ux.clone(record);
            $inited.storeParent = $directory['storePath'];
            state.$inited = $inited;
            state.$window = "renameFile";
            reference.setState(state);
        },
        // 打开目录
        open: () => Ux.fn(reference).rxNav(record),
        // 下载文件
        download: () => Ux.ajaxDownload("/api/file/download/:key", record)
            .then(data => saveAs(data, record.name)),
        // 重命名
        renameDir: () => {
            const state = {};
            state.$visible = true;
            const {$directory = {}} = reference.props;
            const $inited = Ux.clone(record);
            $inited.storeParent = $directory['storePath'];
            state.$inited = $inited;
            state.$window = "renameDir";
            reference.setState(state);
        },
        rollback: (event) => {
            Ux.prevent(event);
            reference.setState({$spinning: true});
            const request = [record];
            // API接口：还原链接，从回收站中还原
            Ux.ajaxPut("/api/document/rollback", request).then(rollback => {
                Ux.messageSuccess(info.rollback);
                let {$selectedKeys = []} = reference.props;
                if ($selectedKeys.includes(record.key)) {
                    $selectedKeys = $selectedKeys.filter(item => item !== record.key);
                }
                Ux.fn(reference).rxMixRollback(rollback, {$selectedKeys});
                reference.setState({$spinning: false});
            })
        },
        // 永久删除
        purge: (event) => {
            Ux.prevent(event);
            // API接口：删除链接，从回收站中彻底删除
            const request = [record];
            Ux.ajaxDelete("/api/document/purge", request).then(purge => {
                Ux.messageSuccess(info.purge);
                let {$selectedKeys = []} = reference.props;
                if ($selectedKeys.includes(record.key)) {
                    $selectedKeys = $selectedKeys.filter(item => item !== record.key);
                }
                Ux.fn(reference).rxMixPurge(purge, {$selectedKeys});
            })
        },
        // 移动到回收站
        remove: () => {
            const content = record.directory ? info.dir : info.file;
            Modal.confirm({
                content,
                // Default Event Will trigger
                onOk: () => {
                    // API接口：回收链接，移动到回收站（批量）
                    const request = [record];
                    Ux.ajaxDelete("/api/document/trash", request).then(removed => {
                        // Delete successfully
                        Ux.messageSuccess(info.finished);
                        let {$selectedKeys = []} = reference.props;
                        if ($selectedKeys.includes(record.key)) {
                            $selectedKeys = $selectedKeys.filter(item => item !== record.key);
                        }
                        Ux.fn(reference).rxMixTrash(removed, {$selectedKeys});
                    })
                }
            })
        }
    })
}
export default {
    yiList,
    yoRowSelection,
    // executor
    rxExecutor,
}